# -*- encoding: utf-8 -*-
"""
@File        : disrtibution_collection_unit.py
@License     : (C)Copyright 2021 LeeRoc (cn) Lipeng
@Modify Time : 2021/7/30 6:06 下午
@Author      : LeeRoc
@Version     : 1.0
@Description : 使用细则

test = LinuxFile('IP', '22', 'ubuntu', 'pwd')
初始化实例对象

test.up_file('./Client.png', '/home/ubuntu/Client.png')
调用函数，切记要把上传到remote的文件名加上

test.down_file('/home/ubuntu/ini.h','./ini.h')
调用函数，切记要把上传到local的文件名加上
"""
import paramiko


class LinuxFile:
    def __init__(self, ip, port, username, password):
        try:
            self.ip = ip
            self.port = port
            self.username = username
            self.password = password
            self.transport = paramiko.Transport((str(self.ip), int(self.port)))
            self.transport.connect(username=self.username, password=self.password)
            self.sftp = paramiko.SFTPClient.from_transport(self.transport)
        except Exception as e:
            raise e

    def up_file(self, localhost_file, server_file):
        try:
            self.sftp.put(localhost_file, server_file)
        except Exception as e:
            raise e

    def down_file(self, localhost_file, server_file):
        try:
            self.sftp.get(localhost_file, server_file)
        except Exception as e:
            raise e

    def close(self):
        try:
            self.transport.close()
        except Exception as e:
            raise e
